package com.example.demo.controller;

import com.example.demo.entity.Cars;
import com.example.demo.service.CarsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cars")
public class CarsController {

    @Autowired
    private CarsService carsService;

    @GetMapping
    public List<Cars> getAllCars() {
        return carsService.list();
    }

    @GetMapping("/{id}")
    public Cars getCarById(@PathVariable Integer id) {
        return carsService.getById(id);
    }

    @PostMapping
    public boolean createCar(@RequestBody Cars car) {
        return carsService.save(car);
    }

    @PutMapping("/{id}")
    public boolean updateCar(@PathVariable Integer id, @RequestBody Cars car) {
        car.setCarId(id); // 假设实体中有setCarId方法
        return carsService.updateById(car);
    }

    @DeleteMapping("/{id}")
    public boolean deleteCar(@PathVariable Integer id) {
        return carsService.removeById(id);
    }
}
